package com.abbas.CayTcore.adapter

import android.text.Editable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.core.widget.addTextChangedListener
import com.abbas.CayTcore.databinding.CounterRvItemBinding
import com.abbas.CayTcore.entity.CounterItem

class CounterListAdapter(
    private var dz: Float,
    private var callback: (weight: Float, counter: Float) -> Unit
) : BaseAdapter() {

    val data: MutableList<CounterItem> = mutableListOf<CounterItem>(
        CounterItem(1, 0f, 0f)
    )

    override fun getCount(): Int {
        return data.size
    }

    override fun getItem(p0: Int): CounterItem {
        return data[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getView(position: Int, p1: View?, p2: ViewGroup?): View {
        val holder: CounterListViewHolder
        val view: View
        if (p1 == null) {
            CounterRvItemBinding.inflate(LayoutInflater.from(p2?.context), p2, false).apply {
                holder = CounterListViewHolder(this)
                view = this.root
                view.tag = holder
            }
        } else {
            p1.apply {
                view = this
                holder = this.tag as CounterListViewHolder
            }
        }

        holder.binding.apply {
            mi.text = Editable.Factory.getInstance().newEditable(data[position].number.toString())
            mi.addTextChangedListener {
                if (!TextUtils.isEmpty(it)) {
                    data[position].number = it.toString().toInt()
                    notifyDataSetChanged()
                }

            }
            weight.text =
                Editable.Factory.getInstance().newEditable(data[position].count.toString())
            weight.addTextChangedListener {
                if (!TextUtils.isEmpty(it)) {
                    it.toString().toFloat().apply {
                        data[position].count = this
                        val computedCounter = computedCounter(this)
                        data[position].much = computedCounter
                        count.text =
                            Editable.Factory.getInstance().newEditable(computedCounter.toString())
                    }
                }
                notifyDataSetChanged()
                onDataChanged()
            }
            count.text = Editable.Factory.getInstance().newEditable(data[position].much.toString())
        }

        return view
    }

    fun addItem() {
        data.add(
            CounterItem(1, 0f, 0f)
        )
        notifyDataSetChanged()
//        notifyItemRangeInserted(data.size - 1, 1)
    }


    fun onDataChanged() {
        var weight = 0f
        var counter = 0f
        data.forEach {
            weight += it.count
            counter += it.much
        }
        callback.invoke(weight, counter)
    }

    private fun computedCounter(weight: Float): Float {
        return weight / dz
    }

    inner class CounterListViewHolder(val binding: CounterRvItemBinding)
}